package com.aaa.ps.util;

import com.aaa.common.entity.SysOperLog;
import com.aaa.common.entity.Users;
import com.aaa.ps.service.LogOperService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * fileName:LogUtil
 * description:公共保存日志的类
 * author:zz
 * createTime:2020/9/1 16:15
 * version:1.0.0
 */
@Component
@Aspect  //切面的实现类，通知类
public class LogUtil {

    @Autowired
    private LogOperService logOperService;

    @Autowired
    private HttpSession httpSession;

    /**
     * 切入点表达式，定义要拦截的业务方法
     */
   // @Pointcut("execution(* com.aaa.ps.service.*.insert*(..))")
    //,execution(* com.aaa.ps.service.*.update*(..))
   @Pointcut("(execution(* com.aaa.ps.service.*.insert*(..))) || (execution(* com.aaa.ps.service.*.update*(..)))|| (execution(* com.aaa.ps.service.*.delete*(..)))")
    public void pointA(){}

    /**
     * 后置通知
     * @param joinPoint
     */
    @AfterReturning(pointcut="pointA()")
    public void saveLog(JoinPoint joinPoint){
        //实例化日志类
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setTitle("权限模块");
        //0其它 1新增 2修改 3删除
        String methodName = joinPoint.getSignature().getName();
        int bType = 0;
        if(methodName.contains("insert")){
            bType=1;
        }else if(methodName.contains("update")){
            bType=2;
        }else{
            bType=3;
        }
        sysOperLog.setBusinessType(bType);
        //获取当前操作对象及方法
        String className = joinPoint.getTarget().getClass().getName();
        sysOperLog.setMethod(className+"."+methodName);
        //0其它 1后台用户 2手机端用户
        sysOperLog.setOperatorType(1);
         Users  users = httpSession.getAttribute("userInfo")==null?null:(Users)httpSession.getAttribute("userInfo");
         //判断
         if(users!=null) {
             sysOperLog.setOperName(users.getUsername());
             sysOperLog.setDeptName(users.getDeptid()+"");
         }
         //获取request对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取请求的Url
        StringBuffer requestURL = request.getRequestURL();
        sysOperLog.setOperUrl(requestURL.toString());
        //获取请求IP地址
        String remoteAddr = request.getRemoteAddr();
        sysOperLog.setOperIp(remoteAddr);
        //获取所有参数
        Object[] args = joinPoint.getArgs();
        String firstArgsName = "";
        //获取第一个参数
        if(args!=null&&args.length>0){
            //使用反射获取第一参数的名称
            firstArgsName =  args[0].getClass().getName();
        }
        if(remoteAddr.contains("0:0:1")||remoteAddr.startsWith("192")||remoteAddr.startsWith("127")) {
            sysOperLog.setOperLocation("内网Ip");
        }
        sysOperLog.setOperParam(firstArgsName);
        //操作状态（0正常 1异常）
        sysOperLog.setStatus(0);
        sysOperLog.setOperTime(new Date());
        logOperService.saveLog(sysOperLog);
    }

}
